BLACK_TEXT="|cff000000";
NORMAL_TEXT="|r";

function OnLoad(self)
	print ("Loading GearBuddy");
	self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED");
end

function OnEvent(self, event, ...)
	if (event == "PLAYER_EQUIPMENT_CHANGED") then
		iLvl();
	end
end

function iLvl()
	--Our frame is a child of the paperdoll frame and each slot now has an overlay text box to hold the 
	--ilvl of the item in its slot
	local invSlot; -- 1 to 18
	local itemID;
	local itemNamesBySlot = {};
	local itemLevelBySlot = {};
	for invSlot=1,19 do
		itemID = GetInventoryItemID("player",invSlot);
		if itemID then
			local itemName,_,_,itemLevel,_,_,_,_,_,_,_ = GetItemInfo(itemID);
			itemNamesBySlot[invSlot] = itemName;
			itemLevelBySlot[invSlot] = itemLevel;
			--print ("Found " .. itemName .. " with ilvl " .. itemLevel .. " in slot " .. invSlot);
		else 
			itemNamesBySlot[invSlot] = "Missing";
                        itemLevelBySlot[invSlot] = 0;
			--print ("Skipping slot " .. invSlot .. " (Nothing there)");
		end
	end
	HeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
	NeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
	ShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
	BackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
	ChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
	ShirtSlotiLvlText:SetFormattedText(itemLevelBySlot[4]);
	TabardSlotiLvlText:SetFormattedText(itemLevelBySlot[19]);
	WristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
	HandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
	WaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
	LegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
	FeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
	Finger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
	Finger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
	Trinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
	Trinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
	MainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
	SecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
	RangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
end
